package com.example.service;

import cn.hutool.json.JSONObject;
import com.example.common.Result;
import com.example.entity.TrainingLog;
import com.example.entity.Trainings;
import com.example.entity.User;
import com.example.mapper.TrainingLogsMapper;
import com.example.mapper.TrainingMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TrainingService {
    @Resource
    TrainingMapper trainingMapper;
    @Resource
    TrainingLogsMapper trainingLogsMapper;
    public Result addTraining(List<Trainings> trainingsList) {
        for(Trainings trainings : trainingsList){
            if(!trainingMapper.add(trainings)){
                return Result.error("插入失败");
            }
        }
        return Result.success("插入成功");
    }

    public Result listTraining(User user) {
        // 获取当前日期的年月日部分
        LocalDate today = LocalDate.now();
        // 将LocalDate转换为Date对象（时间部分为00:00:00）
        Date date = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        List<JSONObject> trainingsList = trainingMapper.listByUser(user);
        List<TrainingLog> trainingLogList = trainingLogsMapper.listAll();
        trainingLogList = trainingLogList.stream()
                .filter(x -> x.getUserId().equals(user.getUserId()))
                .filter(x -> x.getLogDate().equals(date))
                .collect(Collectors.toList());
        Map<Integer,List<TrainingLog>> map = trainingLogList.stream().collect(Collectors.groupingBy(x -> x.getTrainingId()));
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (JSONObject medications : trainingsList){
            if(map.get(Integer.parseInt(medications.get("training_id").toString())) == null){
                medications.put("amount", 0);
            }else {
                medications.put("amount", map.get(Integer.parseInt(medications.get("training_id").toString())).get(0).getLogCount());
            }
            jsonObjectList.add(medications);
        }
        return Result.success(jsonObjectList);
    }

    public Result deleteTraining(Trainings trainings) {
        if(!trainingMapper.deleteTraining(trainings)){
            return Result.error("删除失败");
        }
        List<TrainingLog> trainingLogList = trainingLogsMapper.getByUserIdTrainId(trainings.getTrainingId());
        if(!trainingLogList.isEmpty()){
            TrainingLog trainingLog = new TrainingLog();
            trainingLog.setTrainingId(trainings.getTrainingId());
            trainingLog.setUserId(trainings.getUserId());
            if(!trainingLogsMapper.delete(trainingLog)){
                return Result.error("删除失败");
            }
        }
        return Result.success("删除成功");
    }

    public Result updateTraining(Trainings trainings) {
        if(!trainingMapper.updateTraining(trainings)){
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

    public Result listAll() {
        List<Trainings> trainingsList = trainingMapper.listAll();
        return Result.success(trainingsList);
    }
}
